<script setup lang="ts">
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import { baseURL } from '@/utils/http'
import Close from '@/components/close.vue'
import { postUserCashPrizeAPI } from '@/api/account'
import type { KpcodeBarcodeScanEntity } from '@/types/member'
import { getBarcodeDetailAPI } from '@/api/code'

const form = ref({
  id: '',
  winningSpeech: '',
  winningImages: '',
  kpcodeJackWinningEntity: {
    jackGrade: '',
  },
})
const query = defineProps<{
  id: string
}>()

const detail = ref<KpcodeBarcodeScanEntity>({})
const getBarcodeDetail = async () => {
  if (query.id) {
    const res = await getBarcodeDetailAPI(query.id)
    detail.value = res.data
  }
}

onLoad(() => {
  getBarcodeDetail()
})
// 修改图片
const onAvatarChange = () => {
  // 调用拍照/选择图片
  uni.chooseMedia({
    // 文件个数
    count: 5,
    // 文件类型
    mediaType: ['image'],
    success: (res) => {
      // 本地路径
      const { tempFilePath } = res.tempFiles[0]
      // 文件上传
      uni.uploadFile({
        url: '/admin/sys-file/upload',
        name: 'file', // 后端数据字段名
        filePath: tempFilePath, // 新头像
        success: (res) => {
          // 判断状态码是否上传成功
          if (res.statusCode === 200) {
            // 提取图片地址
            const { url } = JSON.parse(res.data).data
            // 当前页面更新图片信息
            form.value!.winningImages = url
            uni.showToast({ icon: 'success', title: '上传成功' })
          } else {
            uni.showToast({ icon: 'error', title: '出现错误' })
          }
        },
      })
    },
  })
}

// 删除图片
const clearImg = () => {
  form.value!.winningImages = ''
  uni.showToast({ icon: 'success', title: '删除成功' })
}

const onSubmit = async () => {
  // if (form.value.kpcodeJackWinningEntity.jackGrade == '一等奖') {
  //   if (form.value.winningImages != '' && form.value.winningSpeech != '') {
  //     postPrize()
  //   } else {
  //     uni.showToast({ icon: 'none', title: '请输入获奖感言和上传与店家的合影！' })
  //   }
  // } else {
  //   postPrize()
  // }
  postPrize()
}
const postPrize = async () => {
  let obj = {
    id: detail.value.kpcodeJackWinningEntity.id,
    winningSpeech: form.value.winningSpeech,
    // winningImages: form.value.winningImages,
  }
  const res = await postUserCashPrizeAPI(obj)
  if (res.code == '0') {
    uni.showToast({ icon: 'success', title: '提交成功' })
    // form.value.winningImages = ''
    form.value.winningSpeech = ''
    setTimeout(() => {
      uni.redirectTo({ url: '/pages/my/my' })
    }, 2000)
  } else {
    uni.showToast({ icon: 'none', title: res.msg })
  }
}
</script>

<template>
  <!-- 表单 -->
  <view class="form">
    <!-- 表单内容 -->
    <view class="form-content">
      <view class="form-item">
        <text class="label">中奖期号</text>
        <text class="info">{{ detail.jack ? detail.jack.jackPeriod : '暂无' }}</text>
      </view>
      <view class="form-item">
        <text class="label">开奖时间</text>
        <text class="info">{{ detail.jack ? detail.jack.jackStartTime : '暂无' }}</text>
      </view>
      <view class="form-item">
        <text class="label">承销商</text>
        <text class="info">{{ detail.bindBusName }}</text>
      </view>
      <view class="form-item image_upload">
        <text class="label">承销商地址</text>
        <text class="info">{{ detail.bindBusAddress }}</text>
      </view>
      <view class="form-item">
        <text class="label">中奖等级</text>
        <text class="info">{{
          detail.kpcodeJackWinningEntity ? detail.kpcodeJackWinningEntity.jackGrade : '暂无'
        }}</text>
      </view>
      <view class="form-item">
        <text class="label">奖项金额</text>
        <text class="info">{{
          detail.kpcodeJackWinningEntity ? detail.kpcodeJackWinningEntity.jackPrice : '0'
        }}</text>
      </view>
      <view class="form-item">
        <text class="label">税金</text>
        <text class="info">{{
          detail.kpcodeJackWinningEntity.taxation ? detail.kpcodeJackWinningEntity.taxation : '0'
        }}</text>
      </view>
      <view class="form-item">
        <text class="label">到账金额</text>
        <text class="info">{{
          detail.kpcodeJackWinningEntity.winningPrice ? detail.kpcodeJackWinningEntity.winningPrice : '0'
        }}</text>
      </view>
      <view class="form-item">
        <text class="label">乐促码状态</text>
        <view class="info"
          ><view class="tag">{{
            detail.status == '4'
              ? '待中奖'
              : detail.status == '5'
              ? '未中奖'
              : detail.status == '6'
              ? '已中奖'
              : '已过期'
          }}</view></view
        >
      </view>
      <view class="form-item image_upload">
        <text class="label">获奖感言</text>
        <uni-easyinput
          type="textarea"
          autoHeight
          v-model="form.winningSpeech"
          placeholder="请输入内容，非必填"
        ></uni-easyinput>
      </view>
      <!-- <view
        class="form-item image_upload"
        v-if="
          detail.kpcodeJackWinningEntity && detail.kpcodeJackWinningEntity.jackGrade == '一等奖'
        "
      >
        <text class="label">上传合影</text>
        <view v-if="!form.winningImages" class="upload" @tap="onAvatarChange()">
          <uni-icons type="plusempty" color="#ddd" size="40" />
        </view>
        <view v-else class="company_img">
          <image :src="baseURL + form.winningImages" mode="aspectFill" />
          <Close @tap="clearImg()"></Close>
        </view>
      </view> -->
    </view>
    <!-- 提交按钮 -->
    <button class="form-button" @tap="onSubmit">提交审核</button>
  </view>
</template>

<style lang="scss">
.upload {
  width: 300rpx;
  height: 200rpx;
  border: 1px solid #ddd;
  border-radius: 10rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  padding-top: 10rpx;
}

// 表单
.form {
  background-color: #f4f4f4;
  padding-top: 10rpx;
  padding-bottom: 60rpx;

  &-content {
    margin: 20rpx 20rpx 0;
    padding: 0 20rpx;
    border-radius: 10rpx;
    background-color: #fff;
  }

  &-item {
    display: flex;
    height: 96rpx;
    line-height: 46rpx;
    padding: 25rpx 10rpx;
    background-color: #fff;
    font-size: 28rpx;
    border-bottom: 1rpx solid #ddd;

    &:last-child {
      border: none;
    }

    .label {
      width: 180rpx;
      color: #333;
    }

    .account {
      color: #666;
    }

    .input {
      flex: 1;
      display: block;
      height: 46rpx;
    }

    .radio {
      margin-right: 20rpx;
    }

    .picker {
      flex: 1;
    }
    .placeholder {
      color: #808080;
    }
    .code {
      color: #27ba9b;
      font-size: 28rpx;
    }
    .send {
      color: #999;
    }
  }
  .image_upload {
    height: auto;
  }
  &-button {
    height: 80rpx;
    text-align: center;
    line-height: 80rpx;
    margin: 30rpx 20rpx;
    color: #fff;
    border-radius: 80rpx;
    font-size: 30rpx;
    background-color: #27ba9b;
  }
  .tongyi {
    display: flex;
    height: 96rpx;
    line-height: 46rpx;
    padding: 25rpx 10rpx 25rpx 50rpx;
    font-size: 28rpx;
  }
}
</style>
